<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible && !assetAnalysisPlansDetailInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.btn"
        >
          <template v-slot:filter-monthName>
            <el-input
              v-model="filterInfo.query.monthNameMulit"
              class="not-mongolia"
              type="input"
              :required="true"
              :disabled="true"
              :placeholder="'请选择会计期间'"
              style="width: 100%"
            >
              <template #append>
                <el-icon @click="checkFiscalPeriodStart()"
                  ><MoreFilled
                /></el-icon>
              </template>
            </el-input>
          </template>
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-del>
            <el-button :disabled="!canDelete" @click="del()">删除</el-button>
          </template>
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-planNo="scope">
          <span
            v-if="scope.row.planNo"
            class="slip-edit"
            @click="showAnalysisPlansDetail(scope.row)"
            >{{ scope.row.planNo }}</span
          >
          <span v-else>-</span>
        </template>
      </TableContainer>
    </div>
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :orderId="modifyInfo.orderId"
      :title="modifyInfo.title"
      @returnModify="returnModify"
    />
    <!-- 方案明细信息 -->
    <AssetAnalysisPlansDetail
      v-if="assetAnalysisPlansDetailInfo.visible"
      v-model:visible="assetAnalysisPlansDetailInfo.visible"
      :id="assetAnalysisPlansDetailInfo.id"
    />
    <page-dialog
      v-show="editInfo.visible"
      :title="editInfo.title"
      v-model:visible="editInfo.visible"
      :width="editInfo.width"
      :bt-list="editInfo.btList"
    >
      <page-form
        class="paddingBox order-model-formBox flex_wrap_start_start"
        v-model:ref-obj="formInfo.ref"
        :data="formInfo.data"
        :field-list="formInfo.fieldList"
        :rules="formInfo.rules"
        :label-width="formInfo.labelWidth"
      >
        <template v-slot:form-proportion>
          <el-input-number
            style="width: 100%"
            v-model.lazy="formInfo.data.proportion"
            clearable
            controls-position="right"
            :precision="2"
            :min="0.01"
            :max="100"
          />
        </template>
      </page-form>
    </page-dialog>
    <SelectFiscalPeriod
      v-if="selectFiscalPeriodInfo.visible"
      v-model:show-dialog="selectFiscalPeriodInfo.visible"
      :api="getBaseFiscalPeriodPage"
      :save-empty="true"
      :check-box="true"
      :highlight-row="false"
      :query="selectFiscalPeriodInfo.query"
      @callback="callBackSelectFiscalPeriod"
    />
  </div>
</template>

<script setup name="assetConsumables">
import {
  updateAnalyPersonnelCost,
  getAnalyPersonnelCostPage,
  delAnalyPersonnelCost,
} from '@/api/asset'
import { getDeptAll } from '@/api/core'
import { getBaseFiscalPeriodPage } from '@/api/base'
const Modify = defineAsyncComponent(() => import('./modify'))
const AssetAnalysisPlansDetail = defineAsyncComponent(() =>
  import('@/views/asset/analy/assetAnalysisPlans/detail')
)

const { proxy } = getCurrentInstance()
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    monthNameMulit: '',
  },
  copyQuery: {},
  list: [
    {
      slot: 'true',
      label: '会计期间',
      value: 'monthName',
      event: search,
    },
    {
      componentType: 'input',
      label: '方案编号',
      value: 'planNoLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '方案名称',
      value: 'planNameLike',
      event: search,
    },
    {
      label: '分析部门',
      value: 'deptIdMult',
      query: {
        status: 1,
      },
      api: getDeptAll,
      componentType: 'slip-select',
      multiple: true,
      event: search,
    },
    {
      value: 'analysisTypeMult',
      componentType: 'select-model',
      multiple: true,
      code: 'analysis_type',
      label: '分析类型',
      event: search,
    },
    {
      componentType: 'input',
      label: '工号',
      value: 'userNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '姓名',
      value: 'nameLike',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '配置',
      event: create,
      show: proxy.$permission('assetConsumables:add'),
    },
    {
      type: 'slot',
      value: 'del',
      show: proxy.$permission('assetConsumables:del'),
    },
  ],
})

// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getAnalyPersonnelCostPage,
  data: [],
  selections: [],
  fieldList: [
    {
      label: '会计期间',
      value: 'monthName',
    },
    {
      label: '分析方案编码',
      value: 'planNo',
      type: 'slot',
    },
    {
      label: '分析方案名称',
      value: 'planName',
    },
    { label: '分析部门', value: 'deptName' },
    { label: '分析类型', value: 'analysisTypeValue' },
    { label: '工号', value: 'userName' },
    { label: '姓名', value: 'name' },
    { label: '性别', value: 'sexValue' },
    { label: '部门', value: 'userDeptName' },
    { label: '用量占用比例%', value: 'proportion' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '编辑',
        event: update,
        show: proxy.$permission('assetConsumables:update'),
      },
      {
        label: '删除',
        event: del,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('assetConsumables:del'),
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
  id: '',
})
const editInfo = ref({
  visible: false,
  title: '',
  row: {},
  width: '400px',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})
const formInfo = ref({
  ref: null,
  data: {},
  fieldList: [
    {
      label: '用量占用比例%',
      value: 'proportion',
      type: 'slot',
    },
  ],
  rules: {
    proportion: [
      { required: true, message: '请输入用量占用比例', trigger: 'blur' },
    ],
  },
  labelWidth: '140px',
})
const assetAnalysisPlansDetailInfo = ref({
  visible: false,
  id: '',
})
const selectFiscalPeriodInfo = ref({
  visible: false,
  query: {
    fiscalType: 2,
  },
})
const canDelete = computed(() => {
  return tableInfo.value.selections.length
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})
function checkFiscalPeriodStart() {
  selectFiscalPeriodInfo.value.visible = true
}
function callBackSelectFiscalPeriod(data) {
  if (data && data.length) {
    filterInfo.value.query.monthNameMulit = data
      .map((item) => item.name)
      .toString()
    search()
  } else {
    filterInfo.value.query.monthNameMulit = ''
    search()
  }
}
function returnModify() {
  getList()
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 搜索
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//创建
function create() {
  modifyInfo.value.title = '新增人工费用配置'
  modifyInfo.value.visible = true
  modifyInfo.value.id = null
}
//更新
function update(row) {
  editInfo.value.title = '编辑用量占用比例'
  editInfo.value.visible = true
  formInfo.value.data = proxy.$fn.deepClone(row)
}
function handleClose() {
  editInfo.value.visible = false
  formInfo.value.data = {}
}
function checkConfirm() {
  formInfo.value.ref.validate((valid) => {
    if (valid) {
      updateAnalyPersonnelCost(formInfo.value.data).then((res) => {
        if (res && res.success) {
          handleClose()
          getList()
        }
      })
    }
  })
}
//跳转资产卡片详情
function showAnalysisPlansDetail(row) {
  assetAnalysisPlansDetailInfo.value.id = row.analysisPlanId
  assetAnalysisPlansDetailInfo.value.visible = true
}

//删除
function del(row = undefined) {
  if (row && row.id) {
    realDel([row.id])
  } else {
    proxy
      .$confirm('删除后将改变数据，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        let ids = tableInfo.value.selections.map((row) => row.id)
        realDel(ids)
      })
  }
}
function realDel(ids) {
  delAnalyPersonnelCost(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
</script>

<style scoped></style>
